package com.woniu.zsd.shopping.Excel;

import com.woniu.zsd.shopping.model.SalesData;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;

import java.io.FileOutputStream;
import java.io.IOException;
@Component
public class ReportGenerator {
    /**
     * 生成 Excel 报表
     * @param salesData 销售数据对象
     * @param outputFilePath 输出文件路径
     * @throws IOException 如果文件写入失败
     */
    public void generateExcelReport(SalesData salesData, String outputFilePath) throws IOException {
        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("积分报表");

        // 创建表头行
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("今天的订单总数");
        headerRow.createCell(1).setCellValue("今天的销售总积分");
        headerRow.createCell(2).setCellValue("今天的销售数量");

        // 填充数据
        Row dataRow = sheet.createRow(1);
        dataRow.createCell(0).setCellValue(salesData.getTodayOrderCount());
        dataRow.createCell(1).setCellValue(salesData.getTodayPointSum());
        dataRow.createCell(2).setCellValue(salesData.getTodaySellCount());

        // 自动调整列宽
        for (int i = 0; i < 3; i++) {
            sheet.autoSizeColumn(i);
        }

        // 写入文件
        try (FileOutputStream fileOut = new FileOutputStream(outputFilePath)) {
            workbook.write(fileOut);
        } finally {
            workbook.close();
        }
    }
}
